package com.hongyuan.sellcoach.ui.course.activity;

import android.os.Bundle;

import androidx.lifecycle.ViewModelProviders;

import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.AppViewModelFactory;
import com.hongyuan.sellcoach.base.CustomActivity;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.data.md5.BaseUtil;
import com.hongyuan.sellcoach.databinding.ActivitySalesPerformanceBinding;
import com.hongyuan.sellcoach.ui.course.view_mode.SalesPerformanceViewMode;
import com.hongyuan.sellcoach.utils.CustomDialog;

import me.tatarka.bindingcollectionadapter2.BR;

public class SalesPerformanceActivity extends CustomActivity<ActivitySalesPerformanceBinding, SalesPerformanceViewMode> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_sales_performance;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SalesPerformanceViewMode initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SalesPerformanceViewMode.class);
    }

    @Override
    public void initView() {
        super.initView();

        binding.comBack.setOnClickListener(view -> finish());
        setOnRefresh(binding.refresh,REFRESH_0X3);
        setEnableLoadMore(Constants.ORDER_ACHIEVEMENT);
        setEnableRefresh(Constants.ORDER_ACHIEVEMENT);



    }

    @Override
    public void initData() {
        super.initData();

        viewModel.requestData(Constants.ORDER_ACHIEVEMENT);
        viewModel.requestData(Constants.OS_LIST);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showPeriod.observe(this, lists -> {
            CustomDialog.selectDatePeriod(this, (v, message1, message2) -> {
                if("所有时间段".equals(message1)){
                    binding.period.setText(message1);
                    viewModel.clearParams();
                }else{
                    binding.period.setText(message1+"-"+message2);
                    viewModel.setParams("start_time",message1)
                            .setParams("end_time",message2);
                }

                if(binding.osName.getTag() != null && BaseUtil.isValue(binding.osName.getTag())){
                    viewModel.setParams("os_id",String.valueOf(binding.osName.getTag()));
                }

                viewModel.curPage = viewModel.FIRST_PAGE;
                viewModel.requestData(Constants.ORDER_ACHIEVEMENT);

            });
        });


        viewModel.uc.showOs.observe(this,list -> {
            CustomDialog.scroller(this, list, "选择门店", (v, message,id) -> {
                binding.osName.setText(message);
                if("全部门店".equals(message)){
                    binding.osName.setTag("");
                    viewModel.setParams("os_id","");
                }else{
                    viewModel.setParams("os_id",id);
                    binding.osName.setTag(id);
                }
                viewModel.curPage = viewModel.FIRST_PAGE;
                viewModel.requestData(Constants.ORDER_ACHIEVEMENT);
            });
        });
    }
}
